package com.journey.org.ui.search

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.ActivitySearchBinding
import com.journey.org.ui.search.gank.GankSearchFragment
import com.mvvmhabit.org.base.BaseActivity
import com.mvvmhabit.org.base.BaseFragmentPagerAdapter

/**
 * @Date: 2020/11/19 10:03
 * @Author:小妖
 * @Description: 搜索-干货
 */
class SearchActivity : BaseActivity<ActivitySearchBinding, SearchViewModel>() {

    private val mFragment = ArrayList<Fragment>()

    private val mTitle = ArrayList<String>()

    override fun layoutId(savedInstanceState: Bundle?): Int = R.layout.activity_search

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<SearchViewModel> = SearchViewModel::class.java

    override fun initData() {
        super.initData()
        val androidFragment = GankSearchFragment()
        val androidBundle = Bundle()
        androidBundle.putString("type", "Android")
        androidFragment.arguments = androidBundle
        mFragment.add(androidFragment)
        mTitle.add("Android")

        val iosFragment = GankSearchFragment()
        val iosBundle = Bundle()
        iosBundle.putString("type", "IOS")
        iosFragment.arguments = iosBundle
        mFragment.add(iosFragment)
        mTitle.add("IOS")

        val flutterFragment = GankSearchFragment()
        val flutterBundle = Bundle()
        flutterBundle.putString("type", "Flutter")
        flutterFragment.arguments = flutterBundle
        mFragment.add(flutterFragment)
        mTitle.add("Flutter")
        initAdapter()
    }

    private fun initAdapter() {
        mBinding.layoutSearchViewpager.offscreenPageLimit = 1
        val adapter = BaseFragmentPagerAdapter(supportFragmentManager, mFragment, mTitle)
        mBinding.layoutSearchViewpager.adapter = adapter
        mBinding.layoutSearchTab.setupWithViewPager(mBinding.layoutSearchViewpager)
        mBinding.layoutSearchViewpager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(
                mBinding.layoutSearchTab
            )
        )
    }
}
